/**
 * Created by sotto on 2017-5-12.
 */
// ui-select定义
$.fn.UiSearch = function () {
    var ui = $(this);
    $('.ui-search-selected',ui).on('click',function(){
        $('.ui-search-select-list').show();
        return false;
    });
    $('.ui-search-select-list a',ui).on('click',function(){
        $('.ui-search-selected',ui).text($(this).text());
        $('.ui-search-select-list',ui).hide();
        return false;
    });
    $('body').on('click',function(){
        $('.ui-search-select-list').hide();
    });
    $('.ui-search-select-list').hide();

}

// ui-tab处理
// @param {string} content TAB组件的所有选项卡.item
$.fn.UiTab = function () {
    var ui = $(this);
    var tabs = ui.children();
    var cons = ui.siblings();

    tabs.on('click',function () {
        var index = $(this).index();
        tabs.removeClass('content-tab-item-selected')
            .eq(index).addClass('content-tab-item-selected');
        cons.addClass('display-none')
            .eq(index).removeClass('display-none');
        return false;
    });
}

// ui-date处理
$.fn.UiDate = function () {
    var datepp = function (year,month,date) {
        var year2 = parseInt(year), month2 = parseInt(month), date2 = parseInt(date);
        var yearCheck = (year%400==0||(year%100!=0&&year%4==0))?true:false;
        var monthNum;
        switch (month2){
            case 1: case 3: case 5: case 7: case 8: case 10: case 12:
            monthNum = 31;
            break;
            case 4: case 6: case 9: case 11:
            monthNum = 30;
            break;
            case 2: if(yearCheck) monthNum = 29; else monthNum = 28;
                break;
        }
        if(date2+7>monthNum){
            date2 = date2 + 7 - monthNum;
            month2 += 1;
            if(month2 > 12){
                month2 -= 12;
                year2++;
            }
        } else {
            date2 += 7;
        }
        if(month2<10) month2 = '0' + month2;
        if(date2<10) date2 = '0' + date2;
        return year2+'-'+month2+'-'+date2;
    }
    var datedd = function (year,month,date) {
        var year2 = parseInt(year), month2 = parseInt(month)-1, date2 = parseInt(date);
        var yearCheck = (year%400==0||(year%100!=0&&year%4==0))?true:false;
        var monthNum;
        switch (month2){
            case 1: case 3: case 5: case 7: case 8: case 10: case 0:
            monthNum = 31;
            break;
            case 4: case 6: case 9: case 11:
            monthNum = 30;
            break;
            case 2: if(yearCheck) monthNum = 29; else monthNum = 28;
                break;
        }
        month2+=1;
        if(date2-7<1){
            date2 = date2 - 7 + monthNum;
            month2 -= 1;
            if(month2 < 1){
                month2 += 12;
                year2--;
            }
        } else {
            date2 -= 7;
        }
        if(month2<10) month2 = '0' + month2;
        if(date2<10) date2 = '0' + date2;
        return year2+'-'+month2+'-'+date2;

    }

    var ui = $(this);
    $('#content-item-left').on('click',function () {
        var el = $(this);
        for(var i = 0; i < 7; i++){
            el = el.next();
            var elc = el.children('.content-item-date');
            var dateStr = elc.text();
            var year = dateStr.slice(0,4);
            var month = dateStr.slice(5,7);
            var date = dateStr.slice(8,10);
            elc.text(datedd(year, month, date));
        }
    });
    $('#content-item-right').on('click',function () {
        var el = $(this);
        for(var i = 0; i < 7; i++){
            el = el.prev();
            var elc = el.children('.content-item-date');
            var dateStr = elc.text();
            var year = dateStr.slice(0,4);
            var month = dateStr.slice(5,7);
            var date = dateStr.slice(8,10);
            elc.text(datepp(year, month, date));
        }
    });

    // console.log(datepp(year,month,date));
}

// 页面的脚本逻辑
$(function () {


    // var dateStr = $('#content-item-left').next().text().slice(3);
    // var year = dateStr.slice(0,4);
    // var month = dateStr.slice(5,7);
    // var date = dateStr.slice(8,10);
    // console.log(datepp(2000,2,1));
    $('.ui-search').UiSearch();
    $('.content-tab').UiTab();
    $('#content-item-left').UiDate();

});